package com.leo.erp.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.erp.dao.FunctionsMapper;
import com.leo.erp.dto.FunctionsExecution;
import com.leo.erp.entity.Functions;
import com.leo.erp.enums.FunctionsStateEnum;
import com.leo.erp.service.FunctionsService;
import com.leo.erp.service.LogService;
import com.leo.erp.util.GlobalRuntimeException;
import com.leo.erp.util.StringUtil;

@Service
public class FunctionsServiceImpl implements FunctionsService{
	@Autowired
	private FunctionsMapper functionsMapper;
	@Autowired
	private LogService logService;

	@Override
	public List<Functions> getFunctionsList(String pNumber, int enabled, String deleteFlag) {
		
		QueryWrapper<Functions> queryWrapper = new QueryWrapper<>();
		
		if(pNumber != null && pNumber != "") {
			queryWrapper.eq("pNumber", pNumber);
		}
		if(enabled > -1) {
			queryWrapper.eq("enabled", enabled);
		}
		if(deleteFlag != null & deleteFlag != "") {
			queryWrapper.eq("deleteFlag", deleteFlag);
		}
		
		List<Functions> functionsList = new ArrayList<Functions>();
		
		try {
			queryWrapper.orderByAsc("sort");
			functionsList = functionsMapper.selectList(queryWrapper);
		} catch (Exception e) {
			throw new GlobalRuntimeException(e.getMessage());
		}
		
		return functionsList;
	}

	@Override
	public List<Functions> getRoleFunctions(String pNumber) {
		QueryWrapper<Functions> queryWrapper = new QueryWrapper<>();
		if(pNumber != null && pNumber != "") {
			queryWrapper.eq("pNumber", pNumber);
		}
		List<Functions> RoleFunctionsList = new ArrayList<Functions>();
		
		try {
			RoleFunctionsList = functionsMapper.selectList(queryWrapper);
		} catch (Exception e) {
			throw new GlobalRuntimeException(e.getMessage());
		}
		return RoleFunctionsList;
	}

	@Override
	public FunctionsExecution getFunctionsPageList(String name, String type, int currentPage, int pageSize) {
		FunctionsExecution fe = new FunctionsExecution();
		QueryWrapper<Functions> queryWrapper = new QueryWrapper<>();
		
		if(name != null && name != "") {
			queryWrapper.like("name", name);
		}
		if(type != null && type != "") {
			queryWrapper.like("type", type);
		}
		
		Page<Functions> page = new Page<>(currentPage, pageSize);//分页查询
		IPage<Map<String, Object>> iPage = functionsMapper.selectMapsPage(page, queryWrapper);
		if(iPage.getRecords().size() > 0) {
			fe.setState(FunctionsStateEnum.SUCCESS.getState());
			fe.setPages(page.getPages());//总页数
			fe.setTotal(iPage.getTotal());//总记录数
			
			fe.setFunctionsPageList(iPage.getRecords());
		}else{
			fe.setState(FunctionsStateEnum.OFFLINE.getState());
		}
		
		return fe;
	}

	@Override
	public FunctionsExecution deleteFunctions(int functionsId) {
		FunctionsExecution fe = new FunctionsExecution();
		try {
			int effectedNum = functionsMapper.deleteById(functionsId);
			if(effectedNum > 0) {
				fe.setState(FunctionsStateEnum.SUCCESS.getState());
			}else {
				fe.setState(FunctionsStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		return fe;
	}

	@Override
	@Transactional
	public FunctionsExecution insertFunctions(String beanJson)
			throws GlobalRuntimeException {
		Functions functions = JSONObject.parseObject(beanJson, Functions.class);
		functions.setType("电脑版");
		FunctionsExecution fe = new FunctionsExecution();
		try {
			int effectedNum = functionsMapper.insert(functions);
			if(effectedNum > 0) {
				logService.insertLog("功能", "新增" + functions.getName().toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
				fe.setState(FunctionsStateEnum.SUCCESS.getState());
			}else {
				fe.setState(FunctionsStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return fe;
	}

	@Override
	public FunctionsExecution checkIsNameExist(String name) {
		FunctionsExecution fe = new FunctionsExecution();
		QueryWrapper<Functions> queryWrapper = new QueryWrapper<>();
		if(name != null && name != "") {
			queryWrapper.eq("name", name);
		}
		try {
			List<Functions> list = functionsMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				fe.setState(FunctionsStateEnum.SUCCESS.getState());
			}else {
				fe.setState(FunctionsStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return fe;
	}

	@Override
	public FunctionsExecution updateFunctions(Functions functions) {
		FunctionsExecution fe = new FunctionsExecution();
		
		try {
			int effectedNum = functionsMapper.updateById(functions);
			if(effectedNum > 0) {
				logService.insertLog("功能", "修改" + functions.getName().toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
				fe.setState(FunctionsStateEnum.SUCCESS.getState());
			}else {
				fe.setState(FunctionsStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return fe;
	}

	@Override
	public FunctionsExecution findRoleFunctions(String pNumber) {
		FunctionsExecution fe = new FunctionsExecution();
		QueryWrapper<Functions> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("enabled", 1);
		queryWrapper.eq("pNumber", pNumber);
		queryWrapper.ne("deleteFlag", "1");
		queryWrapper.orderByAsc("sort");
		try {
			List<Functions> list = functionsMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				fe.setState(FunctionsStateEnum.SUCCESS.getState());
				fe.setFunctionsList(list);
			}else {
				fe.setState(FunctionsStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return fe;
	}

	@Override
	public FunctionsExecution findByIds(String functionsIds) {
		FunctionsExecution fe = new FunctionsExecution();
		List<Long> idList = StringUtil.strToLongList(functionsIds);
		
		QueryWrapper<Functions> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("enabled", 1);
		queryWrapper.in("functionsId",idList);
		queryWrapper.ne("deleteFlag", "1");
		queryWrapper.orderByAsc("sort");
		
		try {
			List<Functions> list = functionsMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				fe.setState(FunctionsStateEnum.SUCCESS.getState());
				fe.setFunctionsList(list);
			}else {
				fe.setState(FunctionsStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return fe;
	}

}
